<?php
// Connect to DB
mysql_select_db($database, $connection);

// Add queries below

// Session specific queries
if ($_SESSION["loginUsername"]) {
	$query_user = sprintf("SELECT * FROM users WHERE user_name='%s'", $_SESSION["loginUsername"]);
	$user = mysql_query($query_user, $connection) or die(mysql_error());
	$row_user = mysql_fetch_assoc($user);
	$totalRows_user = mysql_num_rows($user);
	
	// -----------------------------------------------------------------------------------------------------
	
	// The following queries are general for adding and editing assignments, readings, and themes ONLY
	// Queries for display of materials, readings, and themes are housed in the following files:
	// -- /sections/modules/display_module_assignments.sec.php
	// -- /sections/modules/display_module_readings.sec.php
	// -- /sections/modules/display_module_themes.sec.php
	
	$query_readings = "SELECT * FROM readings";
	if (($section != "dashboard") && ($action != "edit")) $query_readings .= " WHERE cid='$cid'"; 
	elseif (($mid != "default") && ($action != "edit")) $query_readings .= " AND mid='$mid'";
	elseif (($action == "edit") && ($id != "default")) $query_readings .= " WHERE id='$id'";
	$readings= mysql_query($query_readings, $connection) or die(mysql_error());
	$row_readings = mysql_fetch_assoc($readings);
	$totalRows_readings = mysql_num_rows($readings);
	
	$query_themes = "SELECT * FROM themes";
	if (($section != "dashboard") && (($action != "edit") && ($action != "add") && ($action != "review"))) $query_themes .= " WHERE cid='$cid'"; 
	if (($mid != "default") && (($action != "edit") && ($action != "add") && ($action != "review"))) $query_themes .= " AND mid='$mid'";
	if ((($action == "edit") || ($action == "add") || ($action == "review")) && ($id != "default")) $query_themes .= " WHERE id='$id'"; else $query_themes .= " ORDER BY theme_num ASC";
	$themes = mysql_query($query_themes, $connection) or die(mysql_error());
	$row_themes = mysql_fetch_assoc($themes);
	$totalRows_themes = mysql_num_rows($themes);
	
	$query_assignments = "SELECT * FROM assignments";
	if (($section != "dashboard") && (($action != "edit") && ($action != "add") && ($action != "review"))) $query_assignments .= " WHERE cid='$cid'"; 
	if (($mid != "default") && (($action != "edit") && ($action != "add") && ($action != "review"))) $query_assignments .= " AND mid='$mid'";
	if ((($action == "edit") || ($action == "add") || ($action == "review")) && ($id != "default")) $query_assignments .= " WHERE id='$id'"; else $query_assignments .= " ORDER BY assign_num ASC";
	$assignments = mysql_query($query_assignments, $connection) or die(mysql_error());
	$row_assignments = mysql_fetch_assoc($assignments);
	$totalRows_assignments = mysql_num_rows($assignments);
	
	// -----------------------------------------------------------------------------------------------------
	
	$query_materials = "SELECT * FROM materials";
	if ($section != "dashboard") $query_materials .= " WHERE cid='$cid'"; 
	if ($id != "default") $query_materials .= " AND id='$id'";
	$query_materials .= " ORDER BY matAuthor ASC";
	$materials = mysql_query($query_materials, $connection) or die(mysql_error());
	$row_materials = mysql_fetch_assoc($materials);
	$totalRows_materials = mysql_num_rows($materials);
	
	$query_courses = "SELECT * FROM courses";
	if ($section != "dashboard") $query_courses .= " WHERE id='$cid'"; 
	if (($row_user['userLevel'] <= "2") && ($section == "dashboard")) $query_courses .= sprintf(" WHERE did='%s' AND archive='N'", $row_user['id']);
	if (($row_user['userLevel'] == "3") && ($section == "dashboard")) $query_courses .= sprintf(" WHERE aid='%s' AND archive='N'", $row_user['id']);
	$query_courses .= " ORDER BY jiu_course_id ASC";
	$courses = mysql_query($query_courses, $connection) or die(mysql_error());
	$row_courses = mysql_fetch_assoc($courses);
	$totalRows_courses = mysql_num_rows($courses);
	//echo $query_courses;
	
	$query_cd = "SELECT * FROM course_description";
	if (($section == "view_proposal") && ($id == "default") || ($action == "export") || ($action == "print")) $query_cd .= " WHERE cid='$cid' ORDER BY vid DESC"; 
	if ($id != "default") $query_cd .= " WHERE id='$id'"; 
	$cd = mysql_query($query_cd, $connection) or die(mysql_error());
	$row_cd = mysql_fetch_assoc($cd);
	$totalRows_cd = mysql_num_rows($cd);
	
	$query_pd = "SELECT * FROM project_deliverable WHERE cid='$cid'";
	if (($section == "proposal_titles")|| ($action == "export") || ($action == "print")) $query_pd .= " AND mid='$mid'"; 
	if ($section != "proposal_titles") $query_pd .= " ORDER BY del_num,pd_name ASC";
	$pd = mysql_query($query_pd, $connection) or die(mysql_error());
	$row_pd = mysql_fetch_assoc($pd);
	$totalRows_pd = mysql_num_rows($pd);
	//echo $query_pd;
	
	$query_cpd = "SELECT * FROM course_project";
	if ((($section == "view_proposal") && ($id == "default")) || ($action == "export") || ($action == "print")) $query_cpd .= " WHERE cid='$cid' ORDER BY vid DESC"; 
	if ($id != "default") $query_cpd .= " WHERE id='$id'"; 
	if ($section == "proposal_titles") $query_cpd .= " WHERE cid='$cid'"; 
	$cpd = mysql_query($query_cpd, $connection) or die(mysql_error());
	$row_cpd = mysql_fetch_assoc($cpd);
	$totalRows_cpd = mysql_num_rows($cpd);
	//echo $query_cpd;
	
	$query_modules = "SELECT * FROM modules WHERE cid='$cid'";
	if (($action == "edit") || ($mid != "default")) $query_modules .= " AND mid='$mid'";
	$query_modules .= " ORDER BY mid ASC";
	$modules = mysql_query($query_modules, $connection) or die(mysql_error());
	$row_modules = mysql_fetch_assoc($modules);
	$totalRows_modules = mysql_num_rows($modules);
	
	$query_wp = "SELECT * FROM workplace_comps ORDER BY id ASC";
	$wp = mysql_query($query_wp, $connection) or die(mysql_error());
	$row_wp = mysql_fetch_assoc($wp);
	$totalRows_wp = mysql_num_rows($wp);
	
	$query_rubrics = "SELECT * FROM rubrics ORDER BY rubric_name ASC";
	$rubrics = mysql_query($query_rubrics, $connection) or die(mysql_error());
	$row_rubrics = mysql_fetch_assoc($rubrics);
	$totalRows_rubrics = mysql_num_rows($rubrics);
	
	if ((($section == "view_proposal") || ($section == "comments") || ($section == "view_comments")) && ($id != "default") && ($action != "edit")) { 
		$query_view_specific = "SELECT * FROM $dbTable WHERE id='$id'";
		$view_specific = mysql_query($query_view_specific, $connection) or die(mysql_error());
		$row_view_specific = mysql_fetch_assoc($view_specific);
		$totalRows_view_specific = mysql_num_rows($view_specific);
		
		if ($id != "default") {
			$query_comments = sprintf("SELECT * FROM comments WHERE cid='%s' AND tableName='%s' AND rid='%s'", $cid, $dbTable, $id);
			$comments = mysql_query($query_comments, $connection) or die(mysql_error());
			$row_comments = mysql_fetch_assoc($comments);
			$totalRows_comments = mysql_num_rows($comments);
		
			$query_id = sprintf("SELECT * FROM users WHERE id='%s'", $row_comments['uid']);
			$id = mysql_query($query_id, $connection) or die(mysql_error());
			$row_id = mysql_fetch_assoc($id);
			$totalRows_id = mysql_num_rows($id);
		}
	}
}
?>